package com.rt.easyjava.base.biz1.service.impl.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.easyjava.base.biz1.bean.TmptAuthDeptBiz;
import com.rt.easyjava.base.biz1.dto.TmptAuthDeptBizDto;
import com.rt.easyjava.base.biz1.mapper.TmptAuthDeptBizMapper;
import com.rt.easyjava.base.biz1.service.intf.base.TmptAuthDeptService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-11-28
 */
@Slf4j
public class TmptAuthDeptServiceImpl extends ServiceImpl<TmptAuthDeptBizMapper, TmptAuthDeptBiz> implements TmptAuthDeptService {
    @Autowired
    protected MyMapper mapper;
    @Resource
    protected TmptAuthDeptBizMapper tmptAuthDeptBizMapper;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
    * create a object
    * @param tmptAuthDeptBizDto
    * @return
    */
    @Override
    public Result<TmptAuthDeptBizDto> create(TmptAuthDeptBizDto tmptAuthDeptBizDto) {
        // dto to entity
        TmptAuthDeptBiz tmptAuthDeptBiz = mapper.map(tmptAuthDeptBizDto, TmptAuthDeptBiz.class);

        tmptAuthDeptBizMapper.insert(tmptAuthDeptBiz);

        return Result.builder(tmptAuthDeptBiz.getId());
    }

    /**
    * update a object by Id
    * @param tmptAuthDeptBizDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<TmptAuthDeptBizDto> update(TmptAuthDeptBizDto tmptAuthDeptBizDto, UpdateWrapper updateWrapper) {
        // dto to entity
        TmptAuthDeptBiz tmptAuthDeptBiz = mapper.map(tmptAuthDeptBizDto, TmptAuthDeptBiz.class);

        tmptAuthDeptBizMapper.update(tmptAuthDeptBiz, updateWrapper);

        return Result.builder();
    }

    /**
    * 分页 (无合计功能)
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<TmptAuthDeptBizDto> page(Page<TmptAuthDeptBiz> page, QueryWrapper<TmptAuthDeptBiz> queryWrapper){
        Result<TmptAuthDeptBizDto> result = new Result<>();

        // mapper
        IPage<TmptAuthDeptBiz> iPage = tmptAuthDeptBizMapper.selectPage(page, queryWrapper);

        // 查询结果转换成DTO
        List<TmptAuthDeptBizDto> tmptAuthDeptBizDtoList= mapper.maplist(iPage.getRecords(), TmptAuthDeptBizDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptAuthDeptBizDtoList);

        return result.success();
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptAuthDeptBizDto> selectBy(QueryWrapper<TmptAuthDeptBiz> queryWrapper) {
        Result<TmptAuthDeptBizDto> result = new Result<>();

        List<TmptAuthDeptBiz> list = tmptAuthDeptBizMapper.selectList(queryWrapper);

        List<TmptAuthDeptBizDto> dtoList = mapper.maplist(list, TmptAuthDeptBizDto.class);

        return result.setList(dtoList).success();
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<TmptAuthDeptBizDto> selectById(Long id) {
        Result<TmptAuthDeptBizDto> result = new Result<>();

        TmptAuthDeptBiz tmptAuthDeptBiz = tmptAuthDeptBizMapper.selectById(id);

        TmptAuthDeptBizDto tmptAuthDeptBizDto = mapper.map(tmptAuthDeptBiz, TmptAuthDeptBizDto.class);

        return result.setObj(tmptAuthDeptBizDto).success();
    }



    /**
    * select dto list by key
    * @param columnName
    * @param key
    * @return
    */
    @Override
    public Result<TmptAuthDeptBizDto> selectDtoListByKey(String columnName, String key) {
        Result<TmptAuthDeptBizDto> result = new Result<>();

        List<TmptAuthDeptBiz> tmptAuthDeptBizList = selectListByKey(columnName, key);
        if (tmptAuthDeptBizList == null) {
            return result.fault("no data by key:"+key);
        }

        List<TmptAuthDeptBizDto> tmptAuthDeptBizDtoList = mapper.maplist(tmptAuthDeptBizList, TmptAuthDeptBizDto.class);

        return result.setList(tmptAuthDeptBizDtoList).success();
    }

    /**
    * select bean by key
    * @param columnName
    * @param key
    * @return
    */
    @Override
    public TmptAuthDeptBiz selectByKey(String columnName, String key) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(columnName, key);
        queryWrapper.eq("deleteflag", 0);
        List<TmptAuthDeptBiz> eventRuleBizList = tmptAuthDeptBizMapper.selectList(queryWrapper);
        if (eventRuleBizList.size() == 0) {
            return null;
        }
        return eventRuleBizList.get(0);
    }

    /**
    * select list bean by key
    * @param columnName
    * @param key
    * @return
    */
    @Override
    public List<TmptAuthDeptBiz> selectListByKey(String columnName, String key) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(columnName, key);
        queryWrapper.eq("deleteflag", 0);
        List<TmptAuthDeptBiz> eventRuleBizList = tmptAuthDeptBizMapper.selectList(queryWrapper);
        return eventRuleBizList;
    }






    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * 默认开启合计查询，按需注掉代码
    * @param page
    * @param queryWrapper: 一定要是这个代理工具类才行：QueryWrapper queryWrapper = MyQueryWrapper.buildQueryWrapper(...)
    * @return
    */
    @Override
    public Result<TmptAuthDeptBizDto> myPage(Page<TmptAuthDeptBiz> page, QueryWrapper<TmptAuthDeptBiz> queryWrapper) {
        return this.myPage(page, queryWrapper, true);
    }

    @Override
    public Result<TmptAuthDeptBizDto> myPage(Page<TmptAuthDeptBiz> page, QueryWrapper<TmptAuthDeptBiz> queryWrapper, boolean hasFooter) {
        Result<TmptAuthDeptBizDto> result = new Result();

        IPage<TmptAuthDeptBiz> iPage = tmptAuthDeptBizMapper.myPage(page, queryWrapper);
        // 查询结果转换成DTO
        List<TmptAuthDeptBizDto> tmptAuthDeptBizDtoList = mapper.maplist(iPage.getRecords(), TmptAuthDeptBizDto.class);
        // 返回结果集
        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptAuthDeptBizDtoList);

        // 合计
        if (hasFooter) {
            List<TmptAuthDeptBiz> sumList = tmptAuthDeptBizMapper.mySelectSum(queryWrapper);
            List<TmptAuthDeptBizDto> sumDtoList = mapper.maplist(sumList, TmptAuthDeptBizDto.class);
            result.getMap().put("footer", sumDtoList);
        }

        return result.success();
    }

    /**
    * 查询
    * @param queryWrapper: 一定要是这个代理工具类才行：QueryWrapper queryWrapper = MyQueryWrapper.buildQueryWrapper(...)
    * @return
    */
    @Override
    public Result<TmptAuthDeptBizDto> mySelectBy(QueryWrapper<TmptAuthDeptBiz> queryWrapper) {
        Result<TmptAuthDeptBizDto> result = new Result();

        List<TmptAuthDeptBiz> list = tmptAuthDeptBizMapper.mySelectBy(queryWrapper);
        List<TmptAuthDeptBizDto> dtoList = mapper.maplist(list, TmptAuthDeptBizDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<TmptAuthDeptBizDto> mySelectById(long id) {
        Result<TmptAuthDeptBizDto> result = new Result();

        TmptAuthDeptBiz tmptAuthDeptBiz = tmptAuthDeptBizMapper.mySelectById(id);
        TmptAuthDeptBizDto dto = mapper.map(tmptAuthDeptBiz,TmptAuthDeptBizDto.class);
        result.setObj(dto);

        return result.success();
    }


}
